package com.ujoku.service.admin.impl;

import com.labillusion.core.database.base.mybatis.dao.BaseDao;
import com.labillusion.core.database.base.mybatis.service.base.impl.BaseServiceImpl;
import com.ujoku.context.constants.StatusConstants;
import com.ujoku.context.constants.TaskConstants;
import com.ujoku.dao.admin.TaskDao;
import com.ujoku.dao.admin.UserDao;
import com.ujoku.domain.*;
import com.ujoku.domain.admin.Task;
import com.ujoku.domain.admin.TaskType;
import com.ujoku.domain.admin.User;
import com.ujoku.service.MemberBalanceLogService;
import com.ujoku.service.OrderService;
import com.ujoku.service.RepurchaseItemService;
import com.ujoku.service.admin.TaskService;
import com.ujoku.service.admin.UserService;
import com.ujoku.service.impl.OrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by Greg.Chen on 14-11-1.
 */
@Service
public class TaskServiceImpl extends BaseServiceImpl<Task> implements TaskService<Task> {

    @Autowired
    private TaskDao dao;

    @Override
    protected BaseDao<Task> getBaseDao() {
        return this.dao;
    }

    @Autowired
    private OrderService orderService;

    @Autowired
    private MemberBalanceLogService memberBalanceLogService;

    @Autowired
    private RepurchaseItemService repurchaseItemService;

    @Override
    public void refresh(){
        List<Task> taskList = this.selectList(null);;
        //订单
        Map<String,Object> orderQuery = new HashMap<>();
        orderQuery.put("status", OrderStatus.Processing);
        int orderCount = orderService.selectCount(orderQuery);
        if(orderCount > 0)
        {
            Task orderTask = taskList.stream().filter(t->t.getType().equalsIgnoreCase(TaskType.ORDER)).findFirst().orElse(null);
            if(orderTask != null)
            {
                orderTask.setMessage(String.format(TaskConstants.messageMap.get(TaskType.ORDER),orderCount));
                orderTask.setStatus(StatusConstants.Active);
            }
            this.update(orderTask);
        }
        //提现
        Map<String,Object> memberBalanceLogQuery = new HashMap<>();
        memberBalanceLogQuery.put("type", MemberBalanceLogType.WITHDRAW);
        memberBalanceLogQuery.put("status", MemberBalanceLogStatus.CREATED);
        int withDrawCount = memberBalanceLogService.selectCount(memberBalanceLogQuery);
        if(withDrawCount > 0){
            Task withDrawTask = taskList.stream().filter(t->t.getType().equalsIgnoreCase(TaskType.WITHDRAW)).findFirst().orElse(null);
            if(withDrawTask != null){
                withDrawTask.setMessage(String.format(TaskConstants.messageMap.get(TaskType.WITHDRAW),withDrawCount));
                withDrawTask.setStatus(StatusConstants.Active);
            }

            this.update(withDrawTask);
        }
        //回购
        Map<String,Object> repurchaseQuery = new HashMap<>();
        repurchaseQuery.put("status", RepurchaseStatus.CREATED);
        int repurchaseCount =  repurchaseItemService.getListCountForAdmin(repurchaseQuery);
        if(repurchaseCount > 0){
            Task repurchaseTask = taskList.stream().filter(t->t.getType().equalsIgnoreCase(TaskType.REPURCHASE)).findFirst().orElse(null);
            if(repurchaseTask != null){
                repurchaseTask.setMessage(String.format(TaskConstants.messageMap.get(TaskType.REPURCHASE),repurchaseCount));
                repurchaseTask.setStatus(StatusConstants.Active);
            }

            this.update(repurchaseTask);
        }

    }
}
